//
//  MapSelectionTableViewController.m
//  GoMap
//
//  Created by jsheriff on 2/7/11.
//  Copyright 2011 The MITRE Corporation. All rights reserved.
//

#import "MapSelectionTableViewController.h"
#import "MapPackCustomCell.h"
#import "MapDetailViewController.h"

@implementation MapSelectionTableViewController

#pragma mark -
#pragma mark Initialization

#pragma mark -
#pragma mark View lifecycle

- (void)viewDidLoad {
    [super viewDidLoad];

	self.title = @"Map Packs";
	
//	NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
//	NSString *documentsDirectory = [paths objectAtIndex:0];

//	mapPackLibrary = [[MTMapPackLibrary alloc] initWithPath:documentsDirectory];
	
	UIBarButtonItem *doneButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(closeView)];
    self.navigationItem.leftBarButtonItem = doneButton;
	
//	UIBarButtonItem *addButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(closeView)];
//	self.navigationItem.rightBarButtonItem = addButton;
}

- (void)viewWillAppear:(BOOL)animated {
	if (mapPackLibrary == nil) {
		NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
		NSString *documentsDirectory = [paths objectAtIndex:0];
		mapPackLibrary = [[MTMapPackLibrary alloc] initWithPath:documentsDirectory];
	}
	else {
		[mapPackLibrary refresh];
	}
	
	[self.tableView reloadData];
}

/*
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Return YES for supported orientations.
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
*/


#pragma mark -
#pragma mark Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    // Return the number of sections.
    return 1;
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    // Return the number of rows in the section.
	if (mapPackLibrary == nil) {
		return 0;
	}
	else {
		return [mapPackLibrary.mapPackList count];
	}
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
	// exactly 5 cells (plus navigation bar) displayed at one time
	return 83;
}

//-(UITableViewCellAccessoryType)tableView:(UITableView *)tableView accessoryTypeForRowWithIndexPath:(NSIndexPath *)indexPath {
//	return UITableViewCellAccessoryDetailDisclosureButton;
//}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
	static NSString *CellIdentifier = @"MapPackCustomCell";
	MapPackCustomCell *cell = (MapPackCustomCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
	if (cell == nil) {
		cell = [[[MapPackCustomCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
	}
	
	MTMapPack *mapPack = [mapPackLibrary.mapPackList objectAtIndex:indexPath.row];
	cell.nameLabel.text = mapPack.displayName;
	cell.layersLabel.text = mapPack.layers;
	cell.zoomLabel.text = [NSString stringWithFormat:@"Zoom levels %d to %d", mapPack.minZoom, mapPack.maxZoom];
	if ([indexPath row] == 0)
		cell.selectedImageView.image = [UIImage imageNamed:@"empty_checkbox.PNG"];
	else
		cell.selectedImageView.image = [UIImage imageNamed:@"checked_checkbox.PNG"];
	cell.thumbnailImageView.image = mapPack.thumbnailImage;
	
	return cell;
}


/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/


/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source.
        [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
    }   
    else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view.
    }   
}
*/


/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}
*/


/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/


#pragma mark -
#pragma mark Table view delegate

-(void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath {
	MTMapPack *mapPack = [mapPackLibrary.mapPackList objectAtIndex:indexPath.row];
	
	MapDetailViewController *detailController = [[MapDetailViewController alloc] initWithNibName:@"MapDetailViewController" bundle:nil];
	detailController.mapPack = mapPack;
	[self.navigationController pushViewController:detailController animated:YES];
	[detailController release];
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
	MTMapPack *mapPack = [mapPackLibrary.mapPackList objectAtIndex:indexPath.row];
	NSDictionary *userInfo = [NSDictionary dictionaryWithObject:mapPack.fileName forKey:@"mapPackPath"];
	[[NSNotificationCenter defaultCenter] postNotificationName:kMapSelectionChanged object:self userInfo:userInfo];	
	
	[self closeView];
}

-(IBAction)closeView {
	[self dismissModalViewControllerAnimated:YES];
}


#pragma mark -
#pragma mark Memory management

- (void)didReceiveMemoryWarning {
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Relinquish ownership any cached data, images, etc. that aren't in use.
}

- (void)viewDidUnload {
    // Relinquish ownership of anything that can be recreated in viewDidLoad or on demand.
    // For example: self.myOutlet = nil;
}


- (void)dealloc {
    [super dealloc];
}


@end

